package com.bianyuanren.v4.service.slide.impl;

import java.util.List;

import com.bianyuanren.common.core.text.Convert;
import com.bianyuanren.common.utils.DateUtils;
import com.bianyuanren.framework.util.ShiroUtils;
import com.bianyuanren.v4.service.slide.IPpSlideNumberService;
import com.bianyuanren.v4.mapper.slide.PpSlideNumberMapper;
import com.bianyuanren.v4.mapper.sysset.SysSetMapper;
import com.bianyuanren.v4.pojo.slide.PpSlideNumber;
import com.bianyuanren.v4.pojo.sysset.SysSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 用户滑动记录Service业务层处理
 *
 * @author ruoyi
 * @date 2019-12-10
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class PpSlideNumberServiceImpl implements IPpSlideNumberService {
    @Autowired
    private PpSlideNumberMapper ppSlideNumberMapper;

    @Autowired
    private SysSetMapper sysSetMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询用户滑动记录
     *
     * @param id 用户滑动记录ID
     * @return 用户滑动记录
     */
    @Override
    public PpSlideNumber selectPpSlideNumberById(Long id) {
        return ppSlideNumberMapper.selectPpSlideNumberById(id);
    }

    /**
     * 查询用户滑动记录列表
     *
     * @param ppSlideNumber 用户滑动记录
     * @return 用户滑动记录
     */
    @Override
    public List<PpSlideNumber> selectPpSlideNumberList(PpSlideNumber ppSlideNumber) {
        return ppSlideNumberMapper.selectPpSlideNumberList(ppSlideNumber);
    }

    /**
     * 新增用户滑动记录
     *
     * @param ppSlideNumber 用户滑动记录
     * @return 结果
     */
    @Override
    public int insertPpSlideNumber(PpSlideNumber ppSlideNumber) {
        return ppSlideNumberMapper.insertPpSlideNumber(ppSlideNumber);
    }

    /**
     * 修改用户滑动记录
     *
     * @param ppSlideNumber 用户滑动记录
     * @return 结果
     */
    @Override
    public int updatePpSlideNumber(PpSlideNumber ppSlideNumber) {
        return ppSlideNumberMapper.updatePpSlideNumber(ppSlideNumber);
    }

    /**
     * 删除用户滑动记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePpSlideNumberByIds(String ids) {
        return ppSlideNumberMapper.deletePpSlideNumberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户滑动记录信息
     *
     * @param id 用户滑动记录ID
     * @return 结果
     */
    @Override
    public int deletePpSlideNumberById(Long id) {
        return ppSlideNumberMapper.deletePpSlideNumberById(id);
    }

    /**
     * 获取用户的当前滑动次数
     *
     * @param userId
     */
    @Override
    public PpSlideNumber selectUserById(Long userId) {
        PpSlideNumber ppSlideNumber = ppSlideNumberMapper.selectUserById(userId);
        return ppSlideNumber;
    }

    /**
     * 分享添加滑动次数
     */
    @Override
    public int shareAddNum() {
        SysSet sysSet = sysSetMapper.selectSet();
        PpSlideNumber ppSlideNumber = ppSlideNumberMapper.selectUserById(ShiroUtils.getAppUserId());
        ppSlideNumber.setMaximumSlideNumber(ppSlideNumber.getMaximumSlideNumber() + sysSet.getShareNum());
        return ppSlideNumberMapper.updatePpSlideNumber(ppSlideNumber);
    }

    /**
     * 获取用户每天第一次登陆时原始的数据库的滑动次数
     * 固定滑动次数放入到redis中
     */
    @Override
    public PpSlideNumber settledSlideNumber() {
        //获取当前的用户信息
        Long appUserId = ShiroUtils.getAppUserId();
        String key = "pp:member:settledslide:" + appUserId;
        //判断redis中是否包含对应的key
        if (!redisTemplate.hasKey(key)) {
            //redis中不包含对应的key时从数据库中查询对应的数据放入到redis中
            PpSlideNumber ppSlideNumber = ppSlideNumberMapper.selectUserById(appUserId);
            redisTemplate.opsForHash().put(key, "maximumSlideNumber", ppSlideNumber.getMaximumSlideNumber());
            redisTemplate.opsForHash().put(key, "slideNumber", ppSlideNumber.getSlideNumber());
            redisTemplate.opsForHash().put(key, "date", DateUtils.getDate());
            return ppSlideNumber;
        }else {
            //redis中包含对应的key时判断是否是今天 不是今天
            if (!DateUtils.getDate().equals(redisTemplate.opsForHash().get(key, "date"))){
                PpSlideNumber ppSlideNumber = ppSlideNumberMapper.selectUserById(appUserId);
                //从新赋值
                redisTemplate.opsForHash().put(key, "maximumSlideNumber", ppSlideNumber.getMaximumSlideNumber());
                redisTemplate.opsForHash().put(key, "slideNumber", ppSlideNumber.getSlideNumber());
                redisTemplate.opsForHash().put(key, "date", DateUtils.getDate());
                return ppSlideNumber;
            }
        }
        return null;
    }
}
